<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Form\Selectorsol;
use Application\Form\Carreras;
use Application\Model\Entity\Procesa;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Adapter\Adapter;

class RedactorController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel();
    }
    
    public function carreraAction()
    {
	session_start();
        $form = new Carreras('form');
        $this->dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $this->db2Adapter = $this->getServiceLocator()->get('Application\Db\AdapterTwo');
        
        $datos = $this->request->getPost();
        $datos = $datos->toArray();

//***********************************************************************************************************   
//	  $id_usuario = $data['id_oculto'];
//        $rol_usuario = $data['rol_oculto'];
//        $form->get('id_oculto')->setAttribute('value', $id_usuario);
//        $form->get('rol_oculto')->setAttribute('value', $rol_usuario);
//        $id_usuario = 1;
//        $rol_usuario = 2;
	$id_usuario = $_SESSION['id_usuario'];
	$rol_usuario = $_SESSION['rol_usuario'];
//***********************************************************************************************************
        
        $cedula = $this->dbAdapter->query("SELECT cedula FROM usuarios WHERE id_usuario = $id_usuario", Adapter::QUERY_MODE_EXECUTE);
        $cedula = $cedula->toArray();
        $cedula = $cedula[0][cedula];

//***********************************************************************************************************
	$_SESSION['cedula'] = $cedula;
//***********************************************************************************************************
               
        $datos_alumno = $this->db2Adapter->query("SELECT m.matricula, m.codigo_carrera, c.nombre
                                                     FROM matricula AS m INNER JOIN carrera AS c
                                                     ON m.codigo_carrera = c.codigo_carrera AND m.cedula = $cedula",Adapter::QUERY_MODE_EXECUTE);
        $datos_alumno = $datos_alumno->toArray();
        
        foreach ($datos_alumno as $value)
        {
            $op_matriculas[$value['matricula'].'-'.$value['nombre']] = $value['matricula'].'-'.$value['nombre'];
            //$id_carreras[$value['cod_carrera']] = $value['cod_carrera'];
        }
        
        //print_r($op_matriculas);
        $form->get('matricula')->setValueOptions($op_matriculas);

//***********************************************************************************************************
//        $form->get('id_oculto')->setAttribute("value", $id_usuario);
//        $form->get('rol_oculto')->setAttribute("value", $rol_usuario);
//***********************************************************************************************************

        $titulo = 'Seleccione la Matricula con la que desea realizar la solicitud';
        return new ViewModel(array('titulo'=>$titulo,'form'=>$form, 'url'=>$this->getRequest()->getBaseUrl()));
    }
    
    public function formularioAction()
    {
	//hay que hacer un control mortal aca de que pasa si se ingresa directamente a esta url porque me parece que asi como esta no va a andar
	session_start();
        $form = new Selectorsol('form');
        $this->dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $this->db2Adapter = $this->getServiceLocator()->get('Application\Db\AdapterTwo');
        
        $data = $this->request->getPost();
	//print_r(($data[entrar_pef]));
	if ($data[entrar_pef] == 'Soy Invitado'){
		$id_carrera = 0;
		$solicitudes = $this->dbAdapter->query("SELECT ts.nombre_tipo_solicitud 
		                                        FROM tipo_de_solicitud as ts INNER JOIN solicitud_por_carrera 	as sp ON ts.id_tipo_solicitud = sp.id_tipo_solicitud AND sp.id_carrera = $id_carrera",Adapter::QUERY_MODE_EXECUTE);
		foreach ($solicitudes as $value)
		{
		    $op_solicitudes[$value['nombre_tipo_solicitud']] = $value['nombre_tipo_solicitud'];
		}
		$form->get('solicitud')->setValueOptions($op_solicitudes);
		return new ViewModel(array('titulo'=>'Sea Bienvenido a la Universidad Catolica "Nuestra Señora de la Asunción"', 'form'=>$form, 'url'=>$this->getRequest()->getBaseUrl(), 'redactor'=>"pef"));
	}else{
		$aux = $data['matricula'];
		$aux = explode("-",$aux);
		$matricula = $aux[0];
		$carrera = $aux[1];

//***********************************************************************************************************
//		$id_usuario = $data['id_oculto'];
//		$rol_usuario = $data['rol_oculto'];	
		$_SESSION['carrera'] = $carrera;
		$_SESSION['matricula'] = $matricula;		
//***********************************************************************************************************

		$id_carrera = $this->dbAdapter->query( "SELECT id_carrera FROM carreras WHERE nombre_carrera = '$carrera'",Adapter::QUERY_MODE_EXECUTE);
		$id_carrera = $id_carrera->toArray();
		$id_carrera = $id_carrera[0][id_carrera];
		//print_r($id_carrera);
	
		$solicitudes = $this->dbAdapter->query("SELECT ts.nombre_tipo_solicitud 
		                                        FROM tipo_de_solicitud as ts INNER JOIN solicitud_por_carrera as sp 
		                                        ON ts.id_tipo_solicitud = sp.id_tipo_solicitud AND sp.id_carrera = $id_carrera",Adapter::QUERY_MODE_EXECUTE);
		$solicitudes = $solicitudes->toArray();

		foreach ($solicitudes as $value)
		{
		    $op_solicitudes[$value['nombre_tipo_solicitud']] = $value['nombre_tipo_solicitud'];
		}
		        
		$form->get('solicitud')->setValueOptions($op_solicitudes);

//***********************************************************************************************************
//		$form->get('mat_oculto')->setAttribute('value', $matricula);
//		$form->get('carrera_oculto')->setAttribute('value', $carrera);
//		$form->get('id_oculto')->setAttribute("value", $id_usuario);
//		$form->get('rol_oculto')->setAttribute("value", $rol_usuario);
//***********************************************************************************************************

		$titulo = 'Seleccione el tipo de solicitud que desea realizar';
		return new ViewModel(array('titulo'=>$titulo,'form'=>$form, 'url'=>$this->getRequest()->getBaseUrl(), 'redactor'=>"alumno"));
	}
    }
   
    public function recibeAction()
    {
        $data = $this->request->getPost(); //Todos los datos del formulario tenemos en esta variable
        $procesa = new Procesa($data);
        $datos = $procesa->getData();
        return new ViewModel(array('datos'=>$datos));
    }
}
